package Cplusplus;

/**
 * Created by Martyn on 26.06.14.
 */
public class MyStack
{
    private char hd;
    private MyStack tl;
    private MyStack topOfStack;
    MyStack ()
    {
        hd = 'n';
        tl = null;
        topOfStack = null;
    }
    public char top ()
    {
        if (topOfStack == null)
        {
            System.out.println("Error! top(null)");System.exit(1);
        }
        else return topOfStack.hd;
        return topOfStack.hd;
    }
    public void pop ()
    {
        if (topOfStack == null)
        {
            System.out.println("Error! pop(null");System.exit(1);
        }
        else
        {	MyStack oldTop = topOfStack;
            topOfStack = topOfStack.tl;
        }
    }
    public char pop2()
    {
        if (topOfStack == null)
        {
            System.out.println("Error! pop(null)");System.exit(1);
        }
        else
        {	MyStack oldTop = topOfStack;
            char r = topOfStack.hd;
            topOfStack = topOfStack.tl;
            return r;
        }
        char r = topOfStack.hd;
        return r;
    }
    public void push (char x)
    {
        MyStack p;
        p = topOfStack;
        topOfStack = new MyStack();
        if ( topOfStack != null)
        {
            //topOfStack.hd = new MyStack();
            topOfStack.hd = x;
            topOfStack.tl = p;
        }
        else
        {
            System.out.println("Memory not enough");System.exit(1);
        }
    }
    public boolean isNull()
    {
        return (topOfStack == null) ;
    }
}
